package cn.wolfcode.p2p.business.domain;

import cn.wolfcode.base.BaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.util.BidConst;
import cn.wolfcode.p2p.base.util.JSONUtil;
import cn.wolfcode.p2p.business.util.CalculatetUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.*;

import static cn.wolfcode.p2p.base.util.BidConst.*;

/**
 *    借款对象模型
 *    
 *    @author 
 *    @since 2018-11-28 21:08:52
 */
@Getter@Setter
@Accessors(chain = true)
public class BidRequest extends BaseDomain {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * 乐观锁
     */
    private Integer version = 0;

    /**
     * 借款类型(信用贷)
     */
    private Integer bidRequestType;

    /**
     * 还款类型(等额本息)
     */
    private Integer returnType;

    /**
     * 借款状态
     */
    private int bidRequestState = BidConst.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL;

    /**
     * 借款总金额
     */
    private BigDecimal bidRequestAmount;

    /**
     * 年化利率
     */
    private BigDecimal currentRate;

    /**
     * 最小投標金额
     */
    private BigDecimal minBidAmount;

    /**
     * 借款期数(还款月数)
     */
    private int monthes2return;

    /**
     * 已投标次数(冗余数据)
     */
    private Integer bidCount = 0;

    /**
     * 总回报金额(总利息)
     */
    private BigDecimal totalRewardAmount;

    /**
     * 当前已投标总金额
     */
    private BigDecimal currentSum;

    /**
     * 借款标题
     */
    private String title;

    /**
     * 借款描述
     */
    private String description;

    /**
     * 风控意见
     */
    private String note;

    /**
     * 招标截止日期
     */
    private Date disableDate;

    /**
     * 招标天数
     */
    private Byte disableDays;

    /**
     * 申请时间
     */
    private Date applyTime;

    /**
     * 发标时间
     */
    private Date publishTime;

    /**
     * 借款人ID(还款人)
     */
    private LoginInfo createUser;

    /**
     * 该借款的所有投标记录
     */
    private List<Bid> bids = new ArrayList<>();

    //投标进度
    public int getPersent() {
        return this.currentSum.divide(this.bidRequestAmount, 2, BigDecimal.ROUND_HALF_UP).multiply(CalculatetUtil.ONE_HUNDRED).intValue();
    }


    //还需要的金额
    public BigDecimal getRemainAmount() {
        return this.bidRequestAmount.subtract(this.currentSum);
    }


    public String getJsonString() {
        Map<String, Object> json = new HashMap<>();
        json.put("id", id);
        json.put("username", createUser.getUsername());
        json.put("title", title);
        json.put("bidRequestAmount", bidRequestAmount);
        json.put("currentRate", currentRate);
        json.put("monthes2Return", monthes2return);
        json.put("returnType", this.getDisplayReturnType());
        json.put("totalRewardAmount", totalRewardAmount);

        return JSONUtil.toJSONString(json);
    }

    //还款方式
    public String getDisplayReturnType() {
        return returnType == BidConst.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL ? "等额本息" : "按月到期";
    }

    //借款状态
    public String getDisplayBidRequestState() {
        switch (this.bidRequestState) {
            case BIDREQUEST_STATE_APPLY:
                return "申请中";
            case BIDREQUEST_STATE_PUBLISH_PENDING:
                return "待发布";
            case BIDREQUEST_STATE_BIDDING:
                return "招标中";
            case BIDREQUEST_STATE_UNDO:
                return "已撤销";
            case BIDREQUEST_STATE_BIDDING_OVERDUE:
                return "流标";
            case BIDREQUEST_STATE_APPROVE_PENDING_1:
                return "满标一审";
            case BIDREQUEST_STATE_APPROVE_PENDING_2:
                return "满标二审";
            case BIDREQUEST_STATE_REJECTED:
                return "满标审核被拒";
            case BIDREQUEST_STATE_PAYING_BACK:
                return "还款中";
            case BIDREQUEST_STATE_COMPLETE_PAY_BACK:
                return "完成";
            case BIDREQUEST_STATE_PAY_BACK_OVERDUE:
                return "逾期";
            case BIDREQUEST_STATE_PUBLISH_REFUSE:
                return "发标拒绝";
            default:
                return "";
        }
    }

}